//14.4 - Ship.cpp - Mark Lee - Prima Publishing //#include "ddraw.h" #include "Ship.h" //#include "globals.h" Ship::Ship() //construct a ship object { //load the sprites onto the surface c_pddsone = DDLoadBitmap(g_pdd, "sprites.bmp",800,600); //make the sprite background transparent DDSetColorKey(c_pddsone,RGB(0,255,0)); //calculate and store location of sprites for (int i = 0; i<4;i++) { sprites[i].top = 0; sprites[i].left = i*32; sprites[i].bottom = 31; sprites[i].right = i*32 + 31; } //provide a starting position,direction and speed position.top = 200; position.bottom = 231; position.left = 100; position.right = 131; direction = 3; speed = 4; } BOOL Ship::isCollision(int moveX, int moveY) //test for a collision with land based on //how much the ship will move { //figure out the filename of the current map char* temp; if (currentMapX == 0 && currentMapY == 0) temp = "map1.bmp"; if (currentMapX == 1 && currentMapY == 0) temp = "map2.bmp"; if (currentMapX == 2 && currentMapY == 0) temp = "map3.bmp"; if (currentMapX == 0 && currentMapY == 1) temp = "map4.bmp"; if (currentMapX == 1 && currentMapY == 1) temp = "map5.bmp"; if (currentMapX == 2 && currentMapY == 1) temp = "map6.bmp"; if (currentMapX == 0 && currentMapY == 2) temp = "map7.bmp"; if (currentMapX == 1 && currentMapY == 2) temp = "map8.bmp"; if (currentMapX == 2 && currentMapY == 2) temp = "map9.bmp"; //use landCollision to test return landCollision(position.left%800 + 16 + moveX, position.top%600 + 16 + moveY, temp); } void Ship::Move(int dir) //move the ship { //make this the new direction direction = dir; int moveX; //how much are we going to move? int moveY; //calculate where to move based on direction switch (direction) { case 0: moveX = speed; moveY = 0; break; case 1: moveX = 0; moveY = speed; break; case 2: moveX = -speed; moveY = 0; break; case 3: moveX = 0; moveY = -speed; break; } //if player is moving off of 3x3 map grid then don't move if ((position.bottom + moveY > 1799) || (position.top + moveY < 0)) moveY = 0; if ((position.right + moveX > 2399) || (position.left + moveX < 0)) moveX = 0; //if player is running into land then don't move if (isCollision(moveX, moveY)) { moveX = 0; moveY = 0; } //move the ship position.top += moveY; position.bottom += moveY; position.left += moveX; position.right += moveX; //figure out if we changed maps int tempX = position.left/800; int tempY = position.top /600; //change current map if necessary if (tempX != currentMapX || tempY != currentMapY) { currentMapX = tempX; currentMapY = tempY; } } void Ship::Draw() //draws the game screen { //location is the position of screen to place the ship RECT location = makeRECT(position.left%800, position.top%600, position.right%800, position.bottom%600); //prepare the blitting info DDBLTFX ddbltfx; ZeroMemory(&ddbltfx,sizeof(ddbltfx)); ddbltfx.dwSize = sizeof(ddbltfx); //copy the background map to the screen DDCopyBitmap(g_pddsback, entireMap[currentMapX][currentMapY],0,0,800,600); //write the ship on top, //making the sprites background transparent g_pddsback->Blt(&location,c_pddsone,&sprites[direction], DDBLT_WAIT | DDBLT_KEYSRC,&ddbltfx); //display the screen g_pddsprimary->Flip(NULL,0); } void Ship::moveBack() //moves backwards - used for getting out of town's area { int temp; if (direction == 0) temp = 2; if (direction == 1) temp = 3; if (direction == 2) temp = 0; if (direction == 3) temp = 1; this->Move(temp); }